package com.education.admin.api.controller.system;

import com.education.common.annotation.Param;
import com.education.common.annotation.ParamsType;
import com.education.common.annotation.ParamsValidate;
import com.education.common.utils.Result;
import com.education.service.system.SystemAdminService;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author 余一
 * @Description
 * @date 2021/8/18 12:48
 */
@RestController
@RequestMapping("/system/admin")
public class SystemAdminController {

    @Autowired
    private SystemAdminService systemAdminService;

    /**
     * 获取用户列表
     * @param params
     * @return
     */
    @GetMapping
    @RequiresPermissions("system:admin:list")
    public Result getAllAdmin(@RequestParam Map params){
        return systemAdminService.pagination(params);
    }

    /**
     * 修改或增加用户
     * @param modelMap
     * @return
     */
    @PostMapping
    @ParamsValidate(params = {
            @Param(name = "login_name", message = "请输入账户名称"),
            @Param(name = "roleIds", message = "请选择角色")
    }, paramsType = ParamsType.JSON_DATA)
    @RequiresPermissions(value = {"system:admin:save", "system:admin:update"}, logical = Logical.OR)
    public Result saveOrUpdateAdmin(@RequestBody Map modelMap){
        return systemAdminService.saveOrUpdate(modelMap);
    }

    /**
     * 根据用户id获取角色
     * @param adminId
     * @return
     */
    @GetMapping("/getRoleIdsByAdminId")
    public Result getRoleIdsByAdminId(Integer adminId){
        return systemAdminService.getRoleIdsByAdminId(adminId);
    }

    /**
     * 根据id删除用户
     * @param adminMap
     * @return
     */
    @DeleteMapping("/deleteById")
    @RequiresPermissions("system:admin:deleteById")
    public Result deleteById(@RequestBody Map adminMap){
        return systemAdminService.deleteById(adminMap);
    }

    /**
     * 重置密码
     * @param userInfo
     * @return
     */
    @PostMapping("/resettingPassword")
    @ParamsValidate(params = {
            @Param(name = "password", message = "请输入新密码"),
            @Param(name = "confirmPassword", message = "请输入确认密码")
    }, paramsType = ParamsType.JSON_DATA)
    @RequiresPermissions("system:admin:resettingPassword")
    public Result resettingPassword(@RequestBody Map userInfo){
        return systemAdminService.resettingPassword(userInfo);
    }


}
